package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 *  梅州电视台 解析类
 * 
 * @author Chase
 * 
 */
public class GdmztvVideoAnalyse implements AnalyseNews {

	

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
//		if( url.indexOf("gd") != -1 || url.indexOf("china") != -1|| url.indexOf("zuijinbijiaofan") != -1 ) {
//			return bRet;
//		}
		String[] regex = {
				"http://www.gdmztv.com/.*/.*/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/[0-9]*.html*",
				"http://news.gdmztv.com/[0-9]+/[0-9]+.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}	

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url = urlMeta.getUrl(); 
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
			System.err.println("不符合规则");
		}

		String html = urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title = null;
		String dateStr = null;
		String desc = null;
		Long dateLong = null;
		String commentNum = null;
		String clickNum = null;

		Document doc = Jsoup.parse(html);
		
		dateStr = doc.select(".time").text();
		dateLong = DateUtils.matchDate(dateStr);// 时间
		
		title = doc.select("div.video h1").text();// 标题
		if (StringUtils.isBlank(title)) {
			title = doc.select("title").text();
			title = StringUtils.substringBefore(title, "_");
		}
		
		
		
//		desc = doc.select("div.vinfor>div").last().text(); //描述
//		
//		if(!StringUtils.isNullOrEmpty(desc)) {
//			desc = desc.length()>1000?desc.substring(0, 1000):desc;//小于1000字符的描述
//		}
		
//		clickNum = doc.select("div.vinfor>div>em.hot").text(); //点击数
//		if(StringUtils.isNullOrEmpty(clickNum)) {
//			clickNum = doc.select("em#viewcount").text(); //点击数
//		}
//		System.err.println(clickNum);
		
		NewsMeta meta = new NewsMeta();
		meta.setTitle(title);
		meta.setContent(desc);
		meta.setUrl(url);
		meta.setType(2);
		meta.setClickNum(NumberUtils.parseInt(clickNum));
		meta.setCommentNum(NumberUtils.parseInt(commentNum));
		meta.setDate(dateLong);

		/**
		 * 解析用于更新的地址
		 */
		String updateUrl= null;
		if(updateUrl!=null){
			meta.setUpdateUrl(updateUrl);
			NewsMeta update = Update(meta);
			 return update;
		}
		
		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
//					String commentNumstr = null;
					String clickNumString = null;
					
					String html=responseToURL.getHtml();
					Document doc = Jsoup.parse(html);
//					Elements tdElements = doc.select("div.vinfor>div>em.hot");
					
					clickNumString = doc.select("div.vinfor>div>em.hot").text();
//					commentNumstr = tdElements.text();
//					System.err.println(commentNumstr);
					if(StringUtils.isNullOrEmpty(clickNumString)) {
						clickNumString = doc.select("em#viewcount").text(); //点击数
					}
					
//					meta.setCommentNum(NumberUtils.parseInt(commentNumstr));
					meta.setClickNum(NumberUtils.parseInt(clickNumString));
					return meta;
				}
			}
		}	
		return null;
	}


	
}
